<template>
  <div class="content-wraper">
    <div class="search-box">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="单位名称">
          <el-input v-model="name" />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
        <el-button type="warning" icon="el-icon-refresh" @click="reset()">重置</el-button>
        <el-button type="success" icon="el-icon-plus" @click="addBuildUnit()">新增</el-button>
      </el-form>
    </div>
    <div class="detail">
      <com-table :data="dataList" :columns="columns" :total="total" @pageChange="pageChange">
        <template v-slot:name="{row}">
          <a class="com-link" @click="viewDetail(row)">{{ row.name }}</a>
        </template>
        <template v-slot:status="{row}">
          <a class="com-link" style="text-decoration:underline;" @click="getWorkflowDetail(row)">{{ row.status | formateFlowStatus }}</a>
        </template>
        <template v-slot:createTime="{row: {createTime}}">
          {{ dateFormat(createTime) }}
        </template>
        <template v-slot:options="{row}">
          <el-button type="success" icon="el-icon-edit-outline" size="mini" @click="startProcess(row)">提交</el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editBuildUnit(row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="delBuildUnit(row)">删除</el-button>
        </template>
      </com-table>
    </div>
    <add-supplier
      :show-modal.sync="showModal"
      :opt-type="optType"
      :data="unitDetail"
      :type="type"
      :title="title"
      :type-label="typeLabel"
      @freshList="search"
    />
    <process-start :show-modal.sync="showStartModal" :currentrow="unitDetail" @freshList="search" />
    <workflow-schedule :show-modal.sync="showWorkflowSchedule" :data="workflowScheduleData" />
  </div>
</template>

<script>
import workflowSchedule from '@/components/TreeModal/workflowSchedule'
import { getSupplierInfoList, deleteSupplierInfo } from '@/api/travellingTraderManage'
import moment from 'moment'
import addSupplier from './addSupplier.vue'
export default {
  name: 'BuildUnit',
  components: {
    addSupplier,
    workflowSchedule
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    typeLabel: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      name: '',
      dataList: [],
      total: 0,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      showWorkflowSchedule: false,
      workflowScheduleData: {},
      showStartModal: false,
      showModal: false,
      optType: 'add',
      unitDetail: {},
      columns: [
        {
          label: '编号',
          prop: 'number'
        },
        {
          label: '单位名称',
          prop: 'name',
          slot: 'name'
        },
        {
          label: '联系人姓名',
          prop: 'contactName'
        },
        {
          label: '省份',
          prop: 'province'
        },
        {
          label: '城市',
          prop: 'city'
        },
        {
          label: '流程状态',
          slot: 'status'
        },
        {
          label: '编制人',
          prop: 'founderName'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          slot: 'createTime',
          width: '90'
        },
        {
          fixed: 'right',
          label: '操作',
          width: '260',
          slot: 'options'
        }
      ]
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getWorkflowDetail(data) {
      this.showWorkflowSchedule = true
      this.workflowScheduleData = data
    },
    dateFormat(data) {
      if (data) {
        return moment(data).format('YYYY-MM-DD')
      } else {
        return ''
      }
    },
    startProcess(data) {
      this.unitDetail = data
      this.showStartModal = true
    },
    pageChange(data) {
      this.pageParams = data
      this.getDataList()
    },
    getDataList() {
      const params = {
        ...this.pageParams,
        type: this.type,
        name: this.name
      }
      getSupplierInfoList(params).then(res => {
        const { data, total } = res.data || {}
        if (data) {
          this.dataList = data
          this.total = total - 0
        }
      })
    },
    addBuildUnit() {
      this.optType = 'add'
      this.showModal = true
    },
    editBuildUnit(data) {
      this.optType = 'edit'
      this.unitDetail = data
      this.showModal = true
    },
    viewDetail(data) {
      this.optType = 'detail'
      this.unitDetail = data
      this.showAddFileModal = true
    },
    delBuildUnit(data) {
      this.$confirm('确定要删除该单位吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const { id } = data
        deleteSupplierInfo({
          supplierInfoId: id
        }).then(res => {
          const { code, msg } = res
          if (code === '0001') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getDataList()
          } else {
            this.$message({
              type: 'error',
              message: msg || '删除失败!'
            })
          }
        })
      }).catch(() => {
      })
    },
    search() {
      this.pageParams.pageNum = 1
      this.getDataList()
    },
    reset() {
      this.name = ''
    }

  }
}
</script>

<style lang="scss" scoped>
.search-box {
  height: 50px;
}
.detail{
  height: calc(100% - 50px);
}
</style>
